package com.example.appinting;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.appinting.Beans.doctor;
import com.example.appinting.SQL.MysqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AppointActivity extends Activity implements View.OnClickListener {

    private TextView appointName,appointdepartment;
    private String doctor_name="",doctor_department="",doctor_id="",patient_id="";
    private Button btn1,btn2,btn3,btn4,btn5,btn6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        patient_id = this.getIntent().getStringExtra("patient_id");
        doctor_id = this.getIntent().getStringExtra("doctor_id");
        doctor_name = this.getIntent().getStringExtra("doctor_name");
        doctor_department = this.getIntent().getStringExtra("doctor_department");
        System.out.println(doctor_id+doctor_name+doctor_department);

        setContentView((R.layout.activity_appoint));

        appointName = (TextView) findViewById(R.id.appointName);
        appointdepartment = (TextView) findViewById(R.id.appointdepartment);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);

        appointName.setText(doctor_name);
        appointdepartment.setText(doctor_department);


    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn1){
            lastappoint(1,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }
        else if (v.getId() == R.id.btn2){
            lastappoint(2,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }
        else if (v.getId() == R.id.btn3){
            lastappoint(3,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }
        else if (v.getId() == R.id.btn4){
            lastappoint(4,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }
        else if (v.getId() == R.id.btn5){
            lastappoint(5,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }
        else if (v.getId() == R.id.btn6){
            lastappoint(6,Integer.parseInt(doctor_id),Integer.parseInt(patient_id));
        }

        Toast.makeText(AppointActivity.this, "预约成功!", Toast.LENGTH_SHORT).show();
//        结束当前页面
        finish();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    void lastappoint(int periodId,int doctorId,int patientId){

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "INSERT INTO reservation (doctor_id, patient_id, period_id ,appoint_state) VALUES (" +
                    doctorId + ", " + patientId + ", " + periodId +", 0 )";
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            int x = statement.executeUpdate();
            // 处理查询结果
            if(x == 1){
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
